#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
str builtin 类型的 gtest 测试

测试 str 类型的各种方法和操作，验证生成的 C++ 代码正确性
"""

from gtest import test, EXPECT_EQ, EXPECT_TRUE, EXPECT_FALSE


@test("StringBuiltin", "SplitDefault")
def test_split_default():
    """测试默认分隔符的字符串分割"""
    text: str = "hello world python"
    result = text.split()
    expected = ["hello", "world", "python"]
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "SplitCustomSeparator")
def test_split_custom_separator():
    """测试自定义分隔符的字符串分割"""
    text: str = "apple,banana,orange"
    result = text.split(",")
    expected = ["apple", "banana", "orange"]
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "JoinStrings")
def test_join_strings():
    """测试字符串连接"""
    separator: str = ", "
    words = ["apple", "banana", "orange"]
    result: str = separator.join(words)
    expected: str = "apple, banana, orange"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "ReplaceSubstring")
def test_replace_substring():
    """测试字符串替换"""
    text: str = "hello world"
    result: str = text.replace("world", "python")
    expected: str = "hello python"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "StripWhitespace")
def test_strip_whitespace():
    """测试去除首尾空白字符"""
    text: str = "  hello world  "
    result: str = text.strip()
    expected: str = "hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Lstrip")
def test_lstrip():
    """测试去除左侧空白"""
    text: str = "  hello world  "
    result: str = text.lstrip()
    expected: str = "hello world  "
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "LstripNoWhitespace")
def test_lstrip_no_whitespace():
    """测试左侧去除空白 - 无空白"""
    text: str = "hello"
    result: str = text.lstrip()
    expected: str = "hello"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Rstrip")
def test_rstrip():
    """测试去除右侧空白"""
    text: str = "  hello world  "
    result: str = text.rstrip()
    expected: str = "  hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "RstripNoWhitespace")
def test_rstrip_no_whitespace():
    """测试右侧去除空白 - 无空白"""
    text: str = "hello"
    result: str = text.rstrip()
    expected: str = "hello"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "CombinedLstripRstrip")
def test_combined_lstrip_rstrip():
    """测试组合使用 lstrip 和 rstrip，等价于 strip"""
    text: str = "  hello world  "
    temp: str = text.lstrip()
    result: str = temp.rstrip()
    expected: str = "hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "UpperCase")
def test_upper_case():
    """测试转换为大写"""
    text: str = "hello world"
    result: str = text.upper()
    expected: str = "HELLO WORLD"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "LowerCase")
def test_lower_case():
    """测试转换为小写"""
    text: str = "HELLO WORLD"
    result: str = text.lower()
    expected: str = "hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Startswith")
def test_startswith_true():
    """测试字符串前缀匹配 - 匹配情况"""
    text: str = "hello world"
    result: bool = text.startswith("hello")
    EXPECT_TRUE(result)


@test("StringBuiltin", "StartswithFalse")
def test_startswith_false():
    """测试字符串前缀匹配 - 不匹配情况"""
    text: str = "hello world"
    result: bool = text.startswith("world")
    EXPECT_FALSE(result)


@test("StringBuiltin", "Endswith")
def test_endswith_true():
    """测试字符串后缀匹配 - 匹配情况"""
    text: str = "hello world"
    result: bool = text.endswith("world")
    EXPECT_TRUE(result)


@test("StringBuiltin", "EndswithFalse")
def test_endswith_false():
    """测试字符串后缀匹配 - 不匹配情况"""
    text: str = "hello world"
    result: bool = text.endswith("hello")
    EXPECT_FALSE(result)


@test("StringBuiltin", "FindSubstring")
def test_find_substring_found():
    """测试查找子字符串 - 找到情况"""
    text: str = "hello world"
    result: int = text.find("world")
    expected: int = 6
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "FindSubstringNotFound")
def test_find_substring_not_found():
    """测试查找子字符串 - 未找到情况"""
    text: str = "hello world"
    result: int = text.find("python")
    expected: int = -1
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Count")
def test_count():
    """测试计数子串出现次数"""
    text: str = "hello world hello python hello"
    result: int = text.count("hello")
    expected: int = 3
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "CountNotFound")
def test_count_not_found():
    """测试计数子串未找到"""
    text: str = "hello world"
    result: int = text.count("python")
    expected: int = 0
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "CountOverlap")
def test_count_overlap():
    """测试计数重叠子串"""
    text: str = "aaaa"
    result: int = text.count("aa")
    expected: int = 2
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "StringConcatenation")
def test_string_concatenation():
    """测试字符串连接"""
    str1: str = "hello"
    str2: str = "world"
    result: str = str1 + " " + str2
    expected: str = "hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "StringLength")
def test_string_length():
    """测试字符串长度"""
    text: str = "hello world"
    result: int = len(text)
    expected: int = 11
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "EmptyStringLength")
def test_empty_string_length():
    """测试空字符串长度"""
    text: str = ""
    result: int = len(text)
    expected: int = 0
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "StringEquality")
def test_string_equality():
    """测试字符串相等性"""
    str1: str = "hello"
    str2: str = "hello"
    str3: str = "world"

    EXPECT_TRUE(str1 == str2)
    EXPECT_FALSE(str1 == str3)
    EXPECT_FALSE(str1 != str2)
    EXPECT_TRUE(str1 != str3)


@test("StringBuiltin", "Isdigit")
def test_isdigit():
    """测试是否全为数字"""
    text1: str = "12345"
    text2: str = "hello"
    text3: str = "123abc"
    EXPECT_TRUE(text1.isdigit())
    EXPECT_FALSE(text2.isdigit())
    EXPECT_FALSE(text3.isdigit())


@test("StringBuiltin", "Isalpha")
def test_isalpha():
    """测试是否全为字母"""
    text1: str = "hello"
    text2: str = "12345"
    text3: str = "hello123"
    EXPECT_TRUE(text1.isalpha())
    EXPECT_FALSE(text2.isalpha())
    EXPECT_FALSE(text3.isalpha())


@test("StringBuiltin", "Capitalize")
def test_capitalize():
    """测试首字母大写"""
    text: str = "hello WORLD"
    result: str = text.capitalize()
    expected: str = "Hello world"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Title")
def test_title():
    """测试单词首字母大写"""
    text: str = "hello world python"
    result: str = text.title()
    expected: str = "Hello World Python"
    EXPECT_EQ(result, expected)


@test("StringBuiltin", "Zfill")
def test_zfill():
    """测试数字字符串补零"""
    text1: str = "42"
    text2: str = "-42"
    text3: str = "+42"

    result1: str = text1.zfill(5)
    result2: str = text2.zfill(5)
    result3: str = text3.zfill(5)

    EXPECT_EQ(result1, "00042")
    EXPECT_EQ(result2, "-0042")
    EXPECT_EQ(result3, "+0042")
